﻿using HY.Common;
using HY.Entity;
using HY.Service;
using HY.Web.Controllers;
using HY.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HY.Web.Areas.System.Controllers
{
    public class SysUserController : BaseController
    {
        SysUserService service = new SysUserService();
        // GET: System/SysUser
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult List(int page, int limit)
        {
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var userName = Request.Form["userName"];
                var where = $"SU.IsDeleted=0 AND UserName!='{GlobalConfig.AdminUserName}'";
                if (!string.IsNullOrWhiteSpace(userName))
                    where += $" AND UserName like '%{userName}%'";
                var tuple = service.GetPageList<V_UserList>("SysUser SU LEFT JOIN SysDept SD ON SU.DeptID=SD.ID LEFT JOIN SysRole SR ON SU.RoleID=SR.ID", "SU.ID UserID,SU.UserName,SD.DeptName,SR.RoleName,SU.CreateDate", where, page, limit, "SU.CreateDate desc");
                returnModel = base.GetReturnModel(tuple.Item1, tuple.Item2);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error(new LogInfo() { UserID = UserInfo.UserID, LogType = LogType.Select, LogMessage = "查询角色列表" }, ex);
            }
            return Json(returnModel);
        }

        public ActionResult Add()
        {
            SysDeptService sysDeptService = new SysDeptService();
            ViewBag.DeptList = sysDeptService.EFGetMany<SysDept>(s => !s.IsDeleted).OrderBy(o => o.ID).Select(s => new V_SelectModel() { Value = s.ID.ToString(), Text = s.DeptName });
            SysRoleService sysRoleService = new SysRoleService();
            ViewBag.RoleList=sysRoleService.EFGetMany<SysRole>(w=>!w.IsDeleted).OrderBy(o => o.ID).Select(s => new V_SelectModel() { Value = s.ID.ToString(), Text = s.RoleName });
            return View();
        }
        [HttpPost]
        public JsonResult Add(SysUser model)
        {
            ReturnModel returnModel = new ReturnModel();
            LogInfo logInfo = new LogInfo() { UserID = base.UserInfo.UserID, LogType = LogType.Insert };
            try
            {
                model.Password = EncryptHelper.MD5Encrypt("123456");
                model.CreateUserID = base.UserInfo.UserID;
                model.CreateDate = DateTime.Now;
                returnModel.count = service.Add(model);
                returnModel.msg = "保存成功";
                logInfo.LogMessage = $"用户{returnModel.msg}";
                LogHelper.Info(logInfo);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                returnModel.msg = "保存失败";
                logInfo.LogMessage = $"用户{returnModel.msg}";
                LogHelper.Error(logInfo, ex);
            }
            return Json(returnModel);
        }

        public ActionResult Edit(int? id)
        {
            SysDeptService sysDeptService = new SysDeptService();
            ViewBag.DeptList = sysDeptService.EFGetMany<SysDept>(s => !s.IsDeleted).OrderBy(o => o.ID).Select(s => new V_SelectModel() { Value = s.ID.ToString(), Text = s.DeptName });
            SysRoleService sysRoleService = new SysRoleService();
            ViewBag.RoleList = sysRoleService.EFGetMany<SysRole>(w => !w.IsDeleted).OrderBy(o => o.ID).Select(s => new V_SelectModel() { Value = s.ID.ToString(), Text = s.RoleName });
            var model = service.GetById<SysUser>(id);
            return View(model);
        }

        [HttpPost]
        public JsonResult Edit(SysUser model)
        {
            ReturnModel returnModel = new ReturnModel();
            LogInfo logInfo = new LogInfo() { UserID = base.UserInfo.UserID, LogType = LogType.Update };
            try
            {
                var oldModel = service.GetById<SysUser>(model.ID);
                model.Password = oldModel.Password;
                model.IsDeleted = oldModel.IsDeleted;
                model.CreateDate = oldModel.CreateDate;
                model.CreateUserID = oldModel.CreateUserID;
                model.LastModifyUserID = base.UserInfo.UserID;
                model.LastModifyDate = DateTime.Now;
                returnModel.count = service.Update(model);
                returnModel.msg = "保存成功";
                logInfo.LogMessage = $"用户{returnModel.msg}";
                LogHelper.Info(logInfo);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                returnModel.msg = "保存失败";
                logInfo.LogMessage = $"用户{returnModel.msg}";
                LogHelper.Error(logInfo, ex);
            }
            return Json(returnModel);
        }
    }
}